package sjs.service;

import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Transaction;
 
public final class PMF { 
    private static final PersistenceManagerFactory pmfInstance = 
        JDOHelper.getPersistenceManagerFactory("transactions-optional"); 
 
    private PMF() {} 
 
    public static PersistenceManagerFactory get() { 
        return pmfInstance; 
    } 
    public static void save(Object o){
    	PersistenceManager pm=PMF.get().getPersistenceManager();
	    try{
	    	pm.makePersistent(o); 
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	     	pm.close();
	    }
    }
    public static Object findById(Class  c ,Long id){
    	PersistenceManager pm=PMF.get().getPersistenceManager();
	    try{
	    	return pm.getObjectById(c, id);
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	pm.close();
	    }
	    return null;
    }
    public static void delete(Object o){
    	PersistenceManager pm=PMF.get().getPersistenceManager();
	    try{
 	    	pm.deletePersistent(o); 
 	    }catch(Exception e){
 	    	e.printStackTrace();
	    }finally{
	    	pm.close();
	    }
    }
    public static List getListByQl(String ql){
    	PersistenceManager pm=PMF.get().getPersistenceManager();
    	List resList= null;
	    try{
	    	//if(pm.isClosed())PMF.get().
	    	resList= (List)pm.newQuery(ql).execute(); 
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	//pm.close();
	    }
    	return resList;
    }
}